package com.sihai.ehr.service.impl;

import java.text.MessageFormat;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.sihai.core.DataAccessor;
import com.sihai.core.DynamicQuery;
import com.sihai.core.Limit;
import com.sihai.core.LimitedList;
import com.sihai.core.QueryCreator;
import com.sihai.core.Sorter;
import com.sihai.ehr.model.biz.TrainLecturer;
import com.sihai.ehr.model.biz.TrainRealiaResource;
import com.sihai.ehr.service.TrainRealiaResourceService;

/**
 * 培训教材资源服务实现
 * 
 * @author henry
 * 
 */
@Service("trainRealiaResourceService")
public class TrainRealiaResourceServiceImpl implements TrainRealiaResourceService {

	@Resource
	DataAccessor dataAccessor;
	
	@Override
	public TrainRealiaResource getTrainRealiaResource(Long id) {
		if (id == null || id < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.get(TrainRealiaResource.class, id);
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：获取培训教具资源明细数据库发生异常】【异常说明：+"
					+ e.getMessage()
					+ "】【发生地点：TrainRealiaResourceServiceImpl.getTrainRealiaResource;参数id="
					+ id + " 】");
		}
	}

	@Override
	public LimitedList<TrainRealiaResource> getTrainRealiaResources(final Long organizationId, Limit limit, Sorter sorter) {
		if (organizationId == null || organizationId < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.findByQuery(TrainRealiaResource.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							DynamicQuery dynamicQuery = new DynamicQuery(
									"from TrainRealiaResource t where 1 <> 2");
							dynamicQuery
									.append("and t.organization.treePath like '%,"
											+ organizationId + ",%'");
							
							return dynamicQuery.build(session);
						}
					}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取培训教具资源时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：TrainRealiaResourceServiceImpl.getTrainRealiaResources;参数organizationId="
							+ organizationId + "】");
		}
	}
	
	@Override
	public LimitedList<TrainRealiaResource> getTrainRealiaResourcesByPlanId(
			Long planId, Limit limit, Sorter sorter) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void save(TrainRealiaResource trainRealiaResource) {
		if (trainRealiaResource == null || trainRealiaResource.getOrganization() == null) {
			throw new RuntimeException("【lecturer对象参数异常】");
		}
		try {
			dataAccessor.saveOrUpdate(trainRealiaResource);
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：保培训教具资源对象到数据库发生异常】【异常说明："
					+ e.getMessage() + "】【发生地点：TrainRealiaResourceServiceImpl.save】");
		}
	}

	@Override
	public void deleteTrainRealiaResource(final String ids) {
		if (ids == null || ids.isEmpty()) {
			throw new RuntimeException("【参数异常：ids为空】");
		}
		try {
			dataAccessor.batchUpdate(new QueryCreator() {
				@Override
				public Query createQuery(Session session) {
					return session.createQuery(MessageFormat.format(
							"delete from TrainRealiaResource t where t.id in ({0})",
							ids));
				}
			});
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：移出培训教具资源信息数据库发生异常】【异常说明："
					+ e.getMessage()
					+ "】【发生地点：TrainRealiaResourceServiceImpl.deleteTrainRealiaResource】");
		}
	}

}
